package com.xiaozhen.alarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.alarm.dao.AlarmNotifyUserDao;
import com.xiaozhen.alarm.dao.AlarmStrategyDao;
import com.xiaozhen.alarm.domain.AlarmNotifyUser;
import com.xiaozhen.alarm.domain.AlarmStrategy;
import com.xiaozhen.alarm.service.AlarmStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/25 9:21
 * @description:
 */
@Service
public class AlarmStrategyServiceImpl implements AlarmStrategyService {

    @Autowired
    private AlarmStrategyDao alarmStrategyDao;
    @Autowired
    private AlarmNotifyUserDao alarmNotifyUserDao;

    @Override
    public Page<AlarmStrategy> selectAlarmStrategyList(Page<AlarmStrategy> page) {
        Page<AlarmStrategy> list = alarmStrategyDao.selectAlarmStrategyList(page);
        return list;
    }

    @Override
    public Boolean checkStrategyNameUnique(AlarmStrategy alarmStrategy) {
        QueryWrapper<AlarmStrategy> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("strategy_id");
        queryWrapper.eq("strategy_name", alarmStrategy.getStrategyName());
        List<AlarmStrategy> alarmStrategyList = alarmStrategyDao.selectList(queryWrapper);
        if (alarmStrategyList.size() == 0) {
            return true;
        } else if (alarmStrategy.getStrategyId() != null && alarmStrategy.getStrategyId().equals(alarmStrategyList.get(0).getStrategyId())) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int insertAlarmStrategy(AlarmStrategy alarmStrategy) {
        int num = alarmStrategyDao.insert(alarmStrategy);
        if (num > 0) {
            List<Long> notifyUserIds = alarmStrategy.getNotifyUserIds();
            Long strategyId = alarmStrategy.getStrategyId();
            for (Long notifyUserId : notifyUserIds) {
                AlarmNotifyUser alarmNotifyUser = new AlarmNotifyUser();
                alarmNotifyUser.setStrategyId(strategyId);
                alarmNotifyUser.setUserId(notifyUserId);
                alarmNotifyUserDao.insert(alarmNotifyUser);
            }
        }
        return num;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateAlarmStrategy(AlarmStrategy alarmStrategy) {
        Long strategyId = alarmStrategy.getStrategyId();
        int num = alarmStrategyDao.updateById(alarmStrategy);
        if (num > 0) {
            QueryWrapper<AlarmNotifyUser> notifyUserQueryWrapper = new QueryWrapper<>();
            notifyUserQueryWrapper.eq("strategy_id", strategyId);
            alarmNotifyUserDao.delete(notifyUserQueryWrapper);
            List<Long> notifyUserIds = alarmStrategy.getNotifyUserIds();
            for (Long notifyUserId : notifyUserIds) {
                AlarmNotifyUser alarmNotifyUser = new AlarmNotifyUser();
                alarmNotifyUser.setStrategyId(strategyId);
                alarmNotifyUser.setUserId(notifyUserId);
                alarmNotifyUserDao.insert(alarmNotifyUser);
            }
        }
        return num;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteAlarmStrategyById(Long strategyId) {
        int num = alarmStrategyDao.deleteById(strategyId);
        QueryWrapper<AlarmNotifyUser> notifyUserQueryWrapper = new QueryWrapper<>();
        notifyUserQueryWrapper.eq("strategy_id", strategyId);
        alarmNotifyUserDao.delete(notifyUserQueryWrapper);
        return num;
    }

    @Override
    public List<AlarmStrategy> selectAlarmConfigStrategy(Long configId) {
        return alarmStrategyDao.selectAlarmConfigStrategy(configId);
    }
}
